package cn.rubbit.generate;

import java.io.File;
import java.io.IOException;

import cn.rubbit.ApplicationContext;
import cn.rubbit.entity.Project;
import cn.rubbit.entity.Table;
import cn.rubbit.util.GenerateFile;
import freemarker.template.TemplateException;

public class ProjectGenerate {

	public static void generate() {
		try {
			Project project = (Project) ApplicationContext.get(ApplicationContext.PJ_OBJECT);
			if (project == null) {
				throw new InterruptedException("Project is null");
			}
			String path = project.getPath();
			if (!path.endsWith("\\")) {
				path += "\\";
			}
			path += project.getName() + "\\";
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			if (project.getType().equalsIgnoreCase("war")) {
				GenerateFile.generateFTL(path + ApplicationContext.WEB_TARGET_FILE, ApplicationContext.WEB_FILE, project);
			}
			new File(path + "src\\main\\java").mkdirs();
			new File(path + "src\\main\\resources").mkdirs();
			Table table = new Table();
			table.setPackageName(project.getGroupId() + ".entity");
			GenerateFile.generateFTL(path + ApplicationContext.POM_TARGET_FILE, ApplicationContext.POM_FILE, project);
			path += "src\\main\\java\\" + (project.getGroupId() + ".entity").replaceAll("\\.", "\\\\");
			GenerateFile.generateFTL(path + "\\" + ApplicationContext.BASE_ENTITY_TARGET_FILE, ApplicationContext.BASE_ENTITY_FILE, table);
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
